//
//  GxMainTabBar.swift
//  microblog
//
//  Created by 记忆不为 on 16/9/17.
//  Copyright © 2016年 记忆不为. All rights reserved.
//

import UIKit

class GxMainTabBar: UITabBar {
    var addButtonDidClick:(() ->Void)?
    let count = 5
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let tabBarW = frame.width / CGFloat(count)
        let tabBarH = frame.height
        let tabBarY :CGFloat = 0
        var index = 0
        
        for subview in subviews {
            if subview.isKind(of: NSClassFromString("UITabBarButton")!){
                let tabBarX = CGFloat(index) * tabBarW
                
                subview.frame = CGRect(x: tabBarX, y: tabBarY, width: tabBarW, height: tabBarH)
                index += 1
                if index == 2 {
                    addButton.frame = CGRect(x: CGFloat(index) * tabBarW, y: tabBarY, width: tabBarW, height: tabBarH)
                    index += 1
                }
            }
        }
        
    }
    
    lazy var addButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted);
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        button.addTarget(self, action: #selector(didClick), for: .touchUpInside)
        
        self.addSubview(button)
        
        return button
    }()
    
    func didClick() -> Void {
        addButtonDidClick?()
    }
}
